package com.example.hissys.controller;

import com.example.hissys.dto.AppointmentDetailDTO;
import com.example.hissys.dto.AppointmentRequest;
import com.example.hissys.entity.Appointment;
import com.example.hissys.service.AppointmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import lombok.extern.slf4j.Slf4j;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/appointments")
@CrossOrigin(origins = "*")
@Slf4j
public class AppointmentController {
    @Autowired
    private AppointmentService appointmentService;

    /**
     * 创建预约
     */
    @PostMapping
    public ResponseEntity<?> createAppointment(@RequestBody AppointmentRequest request) {
        try {
            Map<String, Object> result = appointmentService.createAppointment(request);
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            log.error("Failed to create appointment", e);
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }

    /**
     * 获取预约列表
     */
    @GetMapping
    public ResponseEntity<?> getAppointmentList(@RequestParam Integer userId) {
        try {
            List<AppointmentDetailDTO> appointments = appointmentService.getAppointmentList(userId);
            return ResponseEntity.ok(appointments);
        } catch (Exception e) {
            log.error("Failed to get appointment list", e);
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }

    /**
     * 取消预约
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<?> deleteAppointment(@PathVariable Integer id) {
        try {
            appointmentService.cancelAppointment(id);
            return ResponseEntity.ok().build();
        } catch (Exception e) {
            log.error("Failed to cancel appointment", e);
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }

    /**
     * 支付预约
     */
    @PostMapping("/{id}/pay")
    public ResponseEntity<?> payAppointment(@PathVariable Integer id) {
        try {
            appointmentService.payAppointment(id);
            return ResponseEntity.ok().build();
        } catch (Exception e) {
            log.error("Failed to pay for appointment", e);
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }
} 